#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import webapp2

from google.appengine.api import memcache
from google.appengine.api import users
from lxml import etree
from pykml.factory import KML_ElementMaker as KML

class MainHandler(webapp2.RequestHandler):
    def get(self):

        #Get a user
        cur_user = users.get_current_user()
        if cur_user:
            import units
            import user
            r = units.Radar()
            r.location.lat = 4
            r.location.lon = 4
            h1 = units.RadarHit()
            h1.emitterLocation.lat=3.9
            h1.emitterLocation.lon=4
            h1.collectorLocation.lat = r.location.lat
            h1.collectorLocation.lon = r.location.lon
            h1.emitterAltitude = 10000
            h1.put()
            h2 = units.RadarHit()
            h2.emitterLocation.lat=4.1
            h2.emitterLocation.lon=4
            h2.collectorLocation.lat = r.location.lat
            h2.collectorLocation.lon = r.location.lon
            h2.emitterAltitude = 11000
            h2.put()
            h3 = units.RadarHit()
            h3.emitterLocation.lat=4
            h3.emitterLocation.lon=3.9
            h3.collectorLocation.lat = r.location.lat
            h3.collectorLocation.lon = r.location.lon
            h3.emitterAltitude = 9000
            h3.put()
            r.radarHits.append(h1.key())
            r.radarHits.append(h2.key())
            r.radarHits.append(h3.key())
            r.put()
            uobj = user.User(userID=cur_user.user_id())
            uobj.buildingList.append(r.key())
            nick = cur_user.nickname()
            if nick:
                uobj.userName = cur_user.nickname()
            for x in range(0,4):
                r = units.Radar()
                r.location.lat = x
                r.location.lon = x
                r.put()
                uobj.buildingList.append(r.key())
            uobj.put()
            greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)<BR>%s" %
                        (cur_user.nickname(),
                         users.create_logout_url("/"),
                         str(uobj.key())
                         )
                        )

        else:
            greeting = ("<a href=\"%s\">Sign in or register</a>." %
                        users.create_login_url("/"))

        self.response.out.write("<html><body>%s</body></html>" % (greeting))



        #self.response.headers['Content-Type'] = 'application/vnd.google-earth.kml+xml'
        #self.response.headers['Content-Type'] = 'text/xml'
        #self.response.out.write(etree.tostring(placemark))


app = webapp2.WSGIApplication([
                               ('/', MainHandler)
                               ], debug=True)
